package com.mangowang.module.main.ui;
/**
 * 文件名 : AllocaPartnerActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/26 15:53
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.mangowang.lib.common.base.BaseActivity;
import com.mangowang.lib.common.net.Apis;
import com.mangowang.lib.common.net.NetworkManager;
import com.mangowang.lib.common.net.interfaces.OnRequestCallBack;
import com.mangowang.lib.common.proto.Mystock;
import com.mangowang.lib.common.proto.Transfer;
import com.mangowang.lib.common.util.StatusBarUtil;
import com.mangowang.module.main.R;
import com.mangowang.module.main.R2;
import com.mangowang.module.main.adapter.AllocPartnerAdapter;
import com.mangowang.module.main.adapter.AllowBanAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by AndroidIntexh1 on 2018/12/27.
 */

public class AllowBanActivity extends BaseActivity {

    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.back_iv)
    ImageView backIv;
    @BindView(R2.id.tv_title)
    TextView tvTitle;
    private AllowBanAdapter mAdapter;
    private List<Transfer.pattern> datas = new ArrayList<>();
    private String title;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allow_ban);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        initRecyclerView();
        backIv.setOnClickListener(view -> finish());
        title = getIntent().getStringExtra("title");
        initData();
        tvTitle.setText(title);
    }

    private void initData() {

        String type = getIntent().getStringExtra("type");
        switch (type) {
            case "版本":
                NetworkManager.INSTANCE.post(Apis.get_patterns, new OnRequestCallBack() {
                    @Override
                    public void onOk(byte[] response) throws InvalidProtocolBufferException {
                        Transfer.pattern_list data = Transfer.pattern_list.parseFrom(response);
                        List<Transfer.pattern> lists = data.getListList();
                        mAdapter.addData(lists);
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                    }
                });
                break;
            case "品牌":
                Transfer.pattern_list.Builder builder = Transfer.pattern_list.newBuilder();
                builder.setCode(getIntent().getStringExtra("patter"));
                NetworkManager.INSTANCE.post(Apis.get_company, builder.build().toByteArray(),new OnRequestCallBack() {
                    @Override
                    public void onOk(byte[] response) throws InvalidProtocolBufferException {
                        Transfer.pattern_list data = Transfer.pattern_list.parseFrom(response);
                        List<Transfer.pattern> lists = data.getListList();
                        mAdapter.addData(lists);
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                    }
                });
                break;
            case "礼包":
                Transfer.pattern_list.Builder builder1 = Transfer.pattern_list.newBuilder();
                builder1.setCode(getIntent().getStringExtra("code"));
                builder1.setGcCode(getIntent().getStringExtra("gc_code"));
                NetworkManager.INSTANCE.post(Apis.get_goods, builder1.build().toByteArray(), new OnRequestCallBack() {
                    @Override
                    public void onOk(byte[] response) throws InvalidProtocolBufferException {
                        Transfer.pattern_list data = Transfer.pattern_list.parseFrom(response);
                        List<Transfer.pattern> lists = data.getListList();
                        mAdapter.addData(lists);
                    }

                    @Override
                    public void onError(int errorCode, String errorMessage) {
                    }
                });
                break;
        }
    }

    private void initRecyclerView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(mAdapter = new AllowBanAdapter(mContext, datas));
        mAdapter.setEmptyView(R.layout.recycle_empty);
        String type = getIntent().getStringExtra("type");
        switch (type) {
            case "版本":
                mAdapter.setOnItemClickListener((adapter, view, position) -> {
                    Transfer.pattern bean = datas.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("name", bean.getName());
                    intent.putExtra("code", bean.getCode());
                    setResult(300, intent);
                    finish();
                });
                break;
            case "品牌":
                mAdapter.setOnItemClickListener((adapter, view, position) -> {
                    Transfer.pattern bean = datas.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("name", bean.getName());
                    intent.putExtra("code", bean.getCode());
                    setResult(500, intent);
                    finish();
                });
                break;
            case "礼包":
                mAdapter.setOnItemClickListener((adapter, view, position) -> {
                    Transfer.pattern bean = datas.get(position);
                    Intent intent = new Intent();
                    intent.putExtra("name", bean.getName());
                    intent.putExtra("code", bean.getCode());
                    setResult(600, intent);
                    finish();
                });
                break;
        }

    }
}
